/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Admin_AddStore.java
 *
 * Created on Mar 30, 2011, 11:30:56 PM
 */
package admin_gui;

import admin.BusinessLogic.Validation;
import admin.Controller.StoreController;
import java.util.regex.Pattern;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author edaype
 */
public class Admin_AddStore extends javax.swing.JPanel {

    /** Creates new form Admin_AddStore */
    private String storeName;
    private String phone;
    private String aptNo;
    private int stNo;
    private String streetName;
    private String streetNumber;
    private String postCode;
    private String city;
    private String province;
    private String tax;
    private StoreController storeController;
  //  private JPanel mainPanel;
  //  private String apartNumber;

    public Admin_AddStore(JPanel mainPanel) {
        initComponents();
//        this.mainPanel = mainPanel;
        storeController = new StoreController(mainPanel);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ItemPanel = new javax.swing.JPanel();
        StoreLabel = new javax.swing.JLabel();
        PostalLabel = new javax.swing.JLabel();
        StNumberLabel = new javax.swing.JLabel();
        ProvinceLabel = new javax.swing.JLabel();
        StreetLabel = new javax.swing.JLabel();
        PhoneLabel = new javax.swing.JLabel();
        CityLabel = new javax.swing.JLabel();
        TaxLabel = new javax.swing.JLabel();
        CityFIeld = new javax.swing.JTextField();
        StreetField = new javax.swing.JTextField();
        StNumberField = new javax.swing.JTextField();
        StoreField = new javax.swing.JTextField();
        PostalField = new javax.swing.JTextField();
        PhoneField = new javax.swing.JTextField();
        ProvinceCombo = new javax.swing.JComboBox();
        apatNoField = new javax.swing.JTextField();
        StNumberLabel1 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        taxBox = new javax.swing.JComboBox();
        ButtonPanel = new javax.swing.JPanel();
        ResetButton = new javax.swing.JButton();
        AddButton = new javax.swing.JButton();
        AddLabel = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));

        ItemPanel.setBackground(new java.awt.Color(255, 255, 255));

        StoreLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        StoreLabel.setText("Store Name");

        PostalLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        PostalLabel.setText("Postal Code");

        StNumberLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        StNumberLabel.setText("Street Number");

        ProvinceLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        ProvinceLabel.setText("Province");

        StreetLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        StreetLabel.setText("Street");

        PhoneLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        PhoneLabel.setText("Phone Number");

        CityLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        CityLabel.setText("City");

        TaxLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        TaxLabel.setText("Tax Applicable");

        CityFIeld.setFont(new java.awt.Font("Tahoma", 0, 24));

        StreetField.setFont(new java.awt.Font("Tahoma", 0, 24));

        StNumberField.setFont(new java.awt.Font("Tahoma", 0, 24));

        StoreField.setFont(new java.awt.Font("Tahoma", 0, 24));

        PostalField.setFont(new java.awt.Font("Tahoma", 0, 24));

        PhoneField.setFont(new java.awt.Font("Tahoma", 0, 24));

        ProvinceCombo.setFont(new java.awt.Font("Tahoma", 0, 24));
        ProvinceCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Select--", "Alberta", "British Columbia", "Manitoba", "New Brunswick", "Newfoundland and Labrador", "Northwest Territories", "Nova Scotia", "Nunavut", "Ontario", "Prince Edward", "Quebec", "Saskatchewan", "Yukon" }));
        ProvinceCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ProvinceComboActionPerformed(evt);
            }
        });

        apatNoField.setFont(new java.awt.Font("Tahoma", 0, 24));

        StNumberLabel1.setFont(new java.awt.Font("Tahoma", 0, 24));
        StNumberLabel1.setText("Apart Number");

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24));

        taxBox.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        taxBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Select--", "AB", "BC", "MB", "NB", "NF", "NS", "ON", "PE", "QC", "SK" }));

        javax.swing.GroupLayout ItemPanelLayout = new javax.swing.GroupLayout(ItemPanel);
        ItemPanel.setLayout(ItemPanelLayout);
        ItemPanelLayout.setHorizontalGroup(
            ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ItemPanelLayout.createSequentialGroup()
                .addGap(41, 41, 41)
                .addComponent(StoreLabel)
                .addGap(18, 18, 18)
                .addComponent(StoreField, javax.swing.GroupLayout.DEFAULT_SIZE, 526, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(ItemPanelLayout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(StNumberLabel1)
                .addGap(18, 18, 18)
                .addComponent(apatNoField, javax.swing.GroupLayout.DEFAULT_SIZE, 525, Short.MAX_VALUE)
                .addGap(11, 11, 11))
            .addGroup(ItemPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(PhoneLabel)
                    .addComponent(ProvinceLabel)
                    .addComponent(PostalLabel)
                    .addComponent(CityLabel)
                    .addComponent(StreetLabel)
                    .addComponent(StNumberLabel)
                    .addComponent(TaxLabel))
                .addGap(18, 18, 18)
                .addGroup(ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 424, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(StNumberField, javax.swing.GroupLayout.DEFAULT_SIZE, 526, Short.MAX_VALUE)
                    .addComponent(CityFIeld, javax.swing.GroupLayout.DEFAULT_SIZE, 526, Short.MAX_VALUE)
                    .addComponent(StreetField, javax.swing.GroupLayout.DEFAULT_SIZE, 526, Short.MAX_VALUE)
                    .addGroup(ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(taxBox, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(PhoneField, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(ProvinceCombo, javax.swing.GroupLayout.Alignment.LEADING, 0, 324, Short.MAX_VALUE)
                        .addComponent(PostalField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)))
                .addContainerGap())
        );
        ItemPanelLayout.setVerticalGroup(
            ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ItemPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(StoreLabel)
                    .addComponent(StoreField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(apatNoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(StNumberLabel1))
                .addGap(18, 18, 18)
                .addGroup(ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(StNumberLabel)
                    .addComponent(StNumberField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(StreetLabel)
                    .addComponent(StreetField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CityLabel)
                    .addComponent(CityFIeld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PostalLabel)
                    .addComponent(PostalField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ProvinceLabel)
                    .addComponent(ProvinceCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PhoneLabel)
                    .addComponent(PhoneField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(TaxLabel)
                    .addComponent(taxBox, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        ButtonPanel.setBackground(new java.awt.Color(255, 255, 255));

        ResetButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        ResetButton.setText("Reset");
        ResetButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ResetButtonActionPerformed(evt);
            }
        });

        AddButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        AddButton.setText("Add");
        AddButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddButtonActionPerformed(evt);
            }
        });

        AddLabel.setFont(new java.awt.Font("Copperplate Gothic Bold", 2, 60));
        AddLabel.setText("Add Store");

        javax.swing.GroupLayout ButtonPanelLayout = new javax.swing.GroupLayout(ButtonPanel);
        ButtonPanel.setLayout(ButtonPanelLayout);
        ButtonPanelLayout.setHorizontalGroup(
            ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ButtonPanelLayout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addComponent(AddLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 410, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(ResetButton, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(AddButton, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        ButtonPanelLayout.setVerticalGroup(
            ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ButtonPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(AddLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ResetButton, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(AddButton, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ItemPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ButtonPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ItemPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(ButtonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddButtonActionPerformed
        storeName = StoreField.getText();
        phone = PhoneField.getText();
        tax = taxBox.getSelectedItem().toString().trim();
        aptNo = apatNoField.getText();
        streetNumber = StNumberField.getText();
        streetName = StreetField.getText();
        postCode = PostalField.getText();
        province = ProvinceCombo.getSelectedItem().toString().trim();
        city = CityFIeld.getText();
        Validation validation = new Validation();
        if (phone.equals("") || postCode.equals("") || streetName.equals("") ||
                streetNumber.equals("") || city.equals("") || aptNo.equals("") || storeName.equals(
                "")) {
            JOptionPane.showMessageDialog(null, "Please input all information",
                    "Error", JOptionPane.ERROR_MESSAGE);
        } else if (!validation.ValidatePostal(postCode, province)) {
            JOptionPane.showMessageDialog(null, "Invalid Postal Code",
                    "Error", JOptionPane.ERROR_MESSAGE);
        } else if (tax.equals("--Select--")) {
            JOptionPane.showMessageDialog(null, "Please select one tax type",
                    "Error", JOptionPane.ERROR_MESSAGE);
        } else if (province.equals("--Select--")) {
            JOptionPane.showMessageDialog(null, "Please select one province",
                    "Error", JOptionPane.ERROR_MESSAGE);
        } else {
            if (isNumeric(streetNumber) && isNumeric(aptNo) && isNumeric(phone)) {
                stNo = Integer.parseInt(streetNumber);
                boolean str = storeController.addStore(storeName, phone, tax,
                        aptNo, stNo, streetName,
                        postCode, city, province);
                if (str) {
                    jLabel1.setText("Store " + storeName +
                            " has registered successfully");
                    StoreField.setText("");
                    PhoneField.setText("");
                    apatNoField.setText("");
                    StNumberField.setText("");
                    StreetField.setText("");
                    PostalField.setText("");
                    CityFIeld.setText("");
                    taxBox.setSelectedIndex(0);
                    ProvinceCombo.setSelectedIndex(0);
                } else {
                }
            } else {
                JOptionPane.showMessageDialog(null,
                        "Please input numbers for 'apartment Number','Street Number' and 'Phone Number'!",
                        "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_AddButtonActionPerformed

    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }

    private void ProvinceComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ProvinceComboActionPerformed

        JComboBox cb = (JComboBox) evt.getSource();
        province = (String) cb.getSelectedItem();
    }//GEN-LAST:event_ProvinceComboActionPerformed

    private void ResetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ResetButtonActionPerformed
        jLabel1.setText("");
        StoreField.setText("");
        PhoneField.setText("");
        apatNoField.setText("");
        StNumberField.setText("");
        StreetField.setText("");
        PostalField.setText("");
        CityFIeld.setText("");
        taxBox.setSelectedIndex(0);
        ProvinceCombo.setSelectedIndex(0);
    }//GEN-LAST:event_ResetButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddButton;
    private javax.swing.JLabel AddLabel;
    private javax.swing.JPanel ButtonPanel;
    private javax.swing.JTextField CityFIeld;
    private javax.swing.JLabel CityLabel;
    private javax.swing.JPanel ItemPanel;
    private javax.swing.JTextField PhoneField;
    private javax.swing.JLabel PhoneLabel;
    private javax.swing.JTextField PostalField;
    private javax.swing.JLabel PostalLabel;
    private javax.swing.JComboBox ProvinceCombo;
    private javax.swing.JLabel ProvinceLabel;
    private javax.swing.JButton ResetButton;
    private javax.swing.JTextField StNumberField;
    private javax.swing.JLabel StNumberLabel;
    private javax.swing.JLabel StNumberLabel1;
    private javax.swing.JTextField StoreField;
    private javax.swing.JLabel StoreLabel;
    private javax.swing.JTextField StreetField;
    private javax.swing.JLabel StreetLabel;
    private javax.swing.JLabel TaxLabel;
    private javax.swing.JTextField apatNoField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JComboBox taxBox;
    // End of variables declaration//GEN-END:variables
}
